package cn.taketoday.live.handler;

import com.alibaba.fastjson.JSONObject;

import org.apache.commons.lang3.StringUtils;

import cn.taketoday.live.config.GlobalData;
import cn.taketoday.live.data.enums.ShieldMessage;
import cn.taketoday.live.thread.FollowShieldThread;
import cn.taketoday.live.thread.GiftShieldThread;
import cn.taketoday.live.thread.WelcomeShieldThread;
import cn.taketoday.live.tools.ParseIndentityTools;

/**
 * @author TODAY 2021/5/17 23:26
 */
public class AnchorLotStartCommandHandler implements CommandHandler {

  @Override
  public void handle(CommandContext context) throws Exception {
    final JSONObject jsonObject = context.getOriginal();
    if (!StringUtils.isEmpty(GlobalData.USERCOOKIE)) {
      //屏蔽礼物
      if (context.contains(ShieldMessage.is_giftShield)) {
        if (GlobalData.parsethankGiftThread != null
                && !GlobalData.parsethankGiftThread.TFLAG
                && !GlobalData.giftShieldThread.getState().toString().equals("RUNNABLE")) {
          String giftName = ((JSONObject) jsonObject.get("data")).getString("gift_name");
          int time = ((JSONObject) jsonObject.get("data")).getInteger("time");
          if (!StringUtils.isEmpty(giftName)) {
            if (GlobalData.giftShieldThread.getState().toString().equals("TERMINATED")
                    || GlobalData.giftShieldThread.getState().toString()
                    .equals("NEW")) {
              GlobalData.giftShieldThread = new GiftShieldThread();
              GlobalData.giftShieldThread.FLAG = false;
              GlobalData.giftShieldThread.setGiftName(giftName);
              GlobalData.giftShieldThread
                      .setTime(ParseIndentityTools.parseTime(time));
              GlobalData.giftShieldThread.start();
            }
            else {
              GlobalData.giftShieldThread.setTime(time);
              GlobalData.giftShieldThread.setGiftName(giftName);
            }
          }
        }
      }
      //开启天选 屏蔽关注
      if (context.contains(ShieldMessage.is_followShield)) {
        if (!GlobalData.followShieldThread.getState().toString().equals("RUNNABLE")) {
          int time = ((JSONObject) jsonObject.get("data")).getInteger("time");
          if (GlobalData.followShieldThread.getState().toString().equals("TERMINATED")
                  || GlobalData.followShieldThread.getState().toString().equals("NEW")) {
            GlobalData.followShieldThread = new FollowShieldThread();
            GlobalData.followShieldThread.FLAG = false;
            GlobalData.followShieldThread.setTime(time);
            GlobalData.followShieldThread.start();
          }
          else {
            GlobalData.followShieldThread.setTime(time);
          }
        }
      }
      //开启天选 屏蔽欢迎
      if (context.contains(ShieldMessage.is_welcomeShield)) {
        if (!GlobalData.welcomeShieldThread.getState().toString().equals("RUNNABLE")) {
          int time = ((JSONObject) jsonObject.get("data")).getInteger("time");
          if (GlobalData.welcomeShieldThread.getState().toString().equals("TERMINATED")
                  || GlobalData.welcomeShieldThread.getState().toString().equals("NEW")) {
            GlobalData.welcomeShieldThread = new WelcomeShieldThread();
            GlobalData.welcomeShieldThread.FLAG = false;
            GlobalData.welcomeShieldThread.setTime(time);
            GlobalData.welcomeShieldThread.start();
          }
          else {
            GlobalData.welcomeShieldThread.setTime(time);
          }
        }
      }

    }
  }

}
